var workspaceManager_Dialogs = function(){
    //setting alert window
    var self = this;
    var alertWin = null;
    this.openAlertSetting = function(){
		if(GB_Alert==null){
			GB_Alert=new QS_Alert_window();
			GB_Alert.show();
		}else{
			GB_Alert.show();
		}
    };
    //search window
    var searchWin = null;
    this.openSearchDlg = function(){
        if (!rtqWindowGroup) {
            rtqWindowGroup = new Util.WindowGroup(document.body, {}, {});
        }
        if (!searchWin) {
            var QSAdvSearch = null;
            searchWin = new Util.RtqWindow(document.body, {
                windowReady: function(obj){
                    obj.setHtml('<div class="rtsrh" id="rtq_srhdialog"></div>');
                    QSAdvSearch = new RTQ_SearchDialog('rtq_srhdialog', function(){
                        obj.hide();
                    });
                },
                onResized: function(win, width, height){
                    QSAdvSearch.grid.resize(width, height - 235);
                    for (var i in QSAdvSearch.filterTabs) {
                        QSAdvSearch.filterTabs[i].setPanelOutSize(width - 120, 140);
                    }
                }
            }, {
                title: QS_Lang.workspace["dg_dadvanced"],
                width: 450,
                height: 480,
                minWidth: 450,
                minHeight: 450,
				oldBox: {width:450,height:480},
                resizable: true,
                isGlobal: true,
                hasSettingPanel: false,
                closeAction: 'hide'
            });
            rtqWindowGroup.register(searchWin);
        }
        else {
            searchWin.show();
        }
    };
    
    //Open More Dialog
    this.OpenFileWin = null;
    this.openOpenFileWin = function(){
        if (!rtqWindowGroup) {
            rtqWindowGroup = new Util.WindowGroup(document.body, {}, {});
        }
        var qslang=QS_Lang.workspace;
        if (!self.OpenFileWin) {
            self.OpenFileWin = new Util.RtqWindow(document.body, {
                windowReady: function(obj){                    
                    obj.setHtml('<div id="rtq-opendialog"><div class="content"></div><div class="action"><div><a class="qs-ui-btn Apply" href="javascript:void(0)">'+qslang["dg_dapply"]+'</a><a class="qs-ui-btn close" href="javascript:void(0)">'+qslang["dg_dclose"]+'</a></div></div></div>');
                    var El = obj.el;
                    var closeEl = $('a.close', El);
					var applyEl=$('a.Apply', El);
                    var contentEl = $('.content', El);
                    closeEl.bind("click.openFile", function(e){
                        obj.hide();
						QS_FullMarsk.hide();
                        return false;
                    });
					applyEl.bind("click.openFile",function(e){
						if(obj.applyOnclickHandler){
							obj.applyOnclickHandler();
						}
						obj.hide();
						
					});
                    contentEl.bind("click.openFile", function(e){
                        var target = $(e.target);
                        if (target.hasClass('file')) {
							if(target.hasClass("rtq-chkbox-off")){
								target.removeClass("rtq-chkbox-off");
							}
							else if(target.hasClass("rtq-ratio-off")){
							    target.addClass("rtq-ratio");
								obj.hide();
							}
							else{
								target.addClass("rtq-chkbox-off");
							}
                            if (obj.listOnclickHandler) {
                                obj.listOnclickHandler(e.target);
                            }
                        }
                        if (target.hasClass('tab')) {
                            $(this).children('.filelist').hide().eq(target.attr('idx')).show();
                            $(this).find('.tab').removeClass('on');
                            target.addClass('on');
                        }
                        target.blur();
                        return false;
                    });
                    
                },
                beforeClosed: function(){
					QS_FullMarsk.hide();
                }
            }, {
                title: qslang["dg_dopen"],
                top: 80,
                width: 400,
                height: 290,
                resizable: false,
                isGlobal: true,
                hasSettingPanel: false,
                closeAction: 'hide'
            });
            rtqWindowGroup.register(self.OpenFileWin);
            self.OpenFileWin.hide();
        }
        else {
            self.OpenFileWin.show();
        }
        self.OpenFileWin.setFileList = function(list, onclickHandler,onApplyHandler,checkbox){
            var tabHtml = '<ul class="tabs clearfix">';
            var html = '';
            var idx = 0;
			var applyEl=$('a.Apply', self.OpenFileWin.el);
            for (var a in list) {
                tabHtml += '<li><a class="tab' + (idx == 0 ? ' on' : '') + '" href="#" idx="' + idx + '">' + a + '</a></li>';
                var alist = list[a];
				var className="file ";
                html += '<div class="data"><ul class="filelist clearfix" ' + (idx == 0 ? 'style="display:block"' : '') + '>';
				if (checkbox===true) {
					className += "rtq-chkbox";
					applyEl.show();
				}
				else {
					className += "rtq-ratio-off";
					applyEl.hide();
				}
				for (var i = 0, l = alist.length; i < l; i++) {
					var checkboxclass="";
					if(typeof alist[i].checked!='undefined'&& !alist[i].checked){
						checkboxclass=" rtq-chkbox-off";
					}
                    html += '<li><a class="'+className+checkboxclass+'" href="#" cmd="'+ alist[i].cmd +'">' + alist[i].text + '</a></li>';
                }
                html += '</ul></div>';
                idx++;
            }
            tabHtml += '</ul>';
            self.OpenFileWin.el.find('.content').html(tabHtml + html);
			
			self.OpenFileWin.el.find('.content li a').ellipsis(false,0);
            self.OpenFileWin.applyOnclickHandler = onApplyHandler;
            self.OpenFileWin.listOnclickHandler = onclickHandler;
			self.OpenFileWin.checkbox=checkbox;
        };
        self.OpenFileWin.hoverMenu = null;
        self.OpenFileWin.setHoverMenu = function(list, onMenuClick){
            var hoverMenu = new Util.CascadeMenu(self.OpenFileWin.toolPanel, {
                display: false
            }, list, {
                onClick: function(item){
                    onMenuClick(currentFile, item.data.val, item.data.type, self.OpenFileWin);
                }
            });//edited by wind 20100507
            var currentFile = null;
            self.OpenFileWin.el.find('.filelist li').hover(function(e){
                currentFile = $(this).children('a')[0];
                var xy = $(this).children('a').position();
                hoverMenu.setPosition(xy.left + 40, xy.top + 70);
                hoverMenu.show();
            }, function(e){
                if (e.target.tagName.toLowerCase() == 'li') {
                    hoverMenu.hide();
                }
            })
            self.OpenFileWin.hoverMenu = hoverMenu;
        };
        return function(fileList, onclickHandler, hoverMenuItems, onMenuClick,onApplyHandler,windowtitle,isCheckbox){
			if(typeof windowtitle!= 'undefined'){
				self.OpenFileWin.setTitle(windowtitle);	
			}
			if (self.OpenFileWin.hoverMenu) {
                self.OpenFileWin.hoverMenu.clear();
            }
            if (fileList) {
                self.OpenFileWin.setFileList(fileList, onclickHandler,onApplyHandler,isCheckbox||false);
            }
            if (hoverMenuItems) {
                self.OpenFileWin.setHoverMenu(hoverMenuItems, onMenuClick);
            }
            /*
             self.OpenFileWin.setHoverMenu([{
             txt: 'Select',
             val: 'sel'
             },{
             txt: 'Edit',
             val: 'edit'
             }], function(file, cmd){
             alert(file.getAttribute('cmd') + ' ' + cmd);
             });*/
            self.OpenFileWin.show();
            var contentEl = self.OpenFileWin.el.find('.content .data');
            var fileScrollPN = new Util.ScrollPanel(contentEl, {
                showX: false
            });
            contentEl.css({
                height: 190
            });
            fileScrollPN.reSize(378, 188);
        }
    }();

    //Open License Agreement Dialog
    this.openLicenseDialog = function(content){
        var content = content || QSLICENSE;
        var width = xDesktopW * 0.75;
        var height = xDesktopH * 0.75;
        var licenseMask = $('<div class="license-mask"></div>').appendTo(document.body);
        var licenseWin = new Util.RtqWindow(document.body, {
            windowReady: function(obj){
                var qslang=QS_Lang.workspace;
                obj.setHtml('<div id="rtq-licensedialog"><div class="content"><div class="data"></div></div><div class="action"><a class="qs-ui-btn accept" href="javascript:void(0)">'+qslang["dg_dagree"]+'</a><a class="qs-ui-btn unaccept" href="javascript:void(0)">'+qslang["dg_ddecline"]+'</a></div></div>');
                var el = obj.el;
                el.find('.content').height(height - 80);
                var unAcceptEl = el.find('.unaccept');
                var acceptEl = el.find('.accept');
                var contentEl = el.find('.content .data');
                contentEl.html(content);
                var dataScrollPN = new Util.ScrollPanel(contentEl, {
                    showX: false
                });
                dataScrollPN.reSize();
                acceptEl.bind('click.license', function(e){
                    if (Util.confirmObj) {
                        Util.confirmObj.hide();
                        Util.confirmObj.callback(false);
                    }
                    Terminal.setConfig({
                        'HideLicenseDialog': true
                    });
                    licenseMask.hide();
                    acceptEl.unbind('.license');
                    obj.clear();
                });
                unAcceptEl.bind('click.license', function(e){
                    Util.confirm(QS_Lang.text["licensedeclineconfirm"], function(b){
                        if (!b) {
                            return;
                        }
                        else {
                            licenseMask.css('zIndex', 1001);
                            acceptEl.unbind('.license');
                            obj.clear();
                            window.location = 'logout.jsp?tmpid=' + QS_UserTID;
                        }
                    }, {
                        okText: 'YES',
                        okTipText: 'Page will go back to login.',
                        cancelText: 'NO',
                        cancelTipText: 'Go back to license agreement.'
                    
                    });
                });
            },
            beforeClosed: function(){
            }
        }, {
            title: QSLICENSE.Title,
            width: width,
            height: height,
            resizable: false,
            isGlobal: true,
            hasSettingPanel: false,
            closable: false
        });
        rtqWindowGroup.register(licenseWin);
		setTimeout(function(){
			var zIndex = licenseWin.el.css('zIndex');
			licenseMask.css('zIndex', zIndex - 10).show();
		},200);
        
    };
    //Open Screenshot Dialog
    var ScreenshotWin = null;
    this.reloadTimer = null;
    this.reloadCount = null;
    this.setScreenshotImg = function(screenShotURL){
        ScreenshotWin.el.find('.pic img')[0].src = screenShotURL;
    };
    this.reloadImage = function(img){
        clearTimeout(this.reloadTimer);
        this.reloadTimer = -1;
        if (!this.reloadCount) {
            this.reloadCount = 0;
        }
        if (this.reloadCount >= 50) {
            this.reloadCount = 0;
            return;
        }
        this.reloadTimer = setTimeout(function(){
            img.src = img.src.split('?t=')[0] + '?t=' + ((Math.random() * 10000) | 1);
            self.reloadCount++;
        }, 3000);
    };
    this.openScreenshotDialog = function(){
        if (!rtqWindowGroup) {
            rtqWindowGroup = new Util.WindowGroup(document.body, {}, {});
        }
        var qslang=QS_Lang.workspace;
        if (!ScreenshotWin) {
            ScreenshotWin = new Util.RtqWindow(document.body, {
                windowReady: function(obj){
                    obj.setHtml('<div id="rtq-screenshotdialog"><div class="content"><div class="data"><div class="pic"><img src="IM/blank.gif" style="width:400px;height:300px;"/></div></div></div><div class="action"><a class="qs-ui-btn download" href="javascript:void(0)">'+qslang["dg_ddownload"]+'</a><a class="qs-ui-btn close" href="javascript:void(0)">'+qslang["dg_dclose"]+'</a></div></div></div>');
                    var el = obj.el;
                    el.find('.content').height(300);
                    var img = el.find('.content .pic img');
                    var downloadBtn = el.find('.action .download');
                    el.find('.action .close').bind('click', function(e){
                        obj.hide();
                    });
                    downloadBtn.bind('click', function(){
                        return false;
                    });
                    img.bind('load', function(e){
                        var imgUrl = $(this).attr('src');
                        if (imgUrl != "IM/blank.gif") {
                            downloadBtn.unbind('click');
                            this.style.display = 'block';
                            imgUrl = imgUrl.split("?")[0];
                            downloadBtn.attr('href', 'download.jsp?imgUrl=' + imgUrl + "&tmpid=" + QS_UserTID);
                            img.bind('click', function(e){
                                window.open(imgUrl);
                            });
                        }
                    }).bind('error', function(){
                        img.unbind('click');
                        img.hide();
                        downloadBtn.bind('click', function(){
                            return false;
                        });
                        self.reloadImage(this);
                    });
                },
                beforeClosed: function(){
                }
            }, {
                title: qslang["dg_dscreen"],
                width: 422,
                height: 380,
                resizable: false,
                isGlobal: true,
                hasSettingPanel: false,
                closeAction: 'hide'
            });
            rtqWindowGroup.register(ScreenshotWin);
        }
        else {
            var img = ScreenshotWin.el.find('.pic img');
            ScreenshotWin.el.find('.action .download').attr('href', 'javascript:void(0)').bind('click', function(){
                return false;
            });
            img[0].src = 'IM/blank.gif';
            img.unbind('click');
            ScreenshotWin.show();
        }
    };
    
    //Open Tips Dialog
    var TipsWin = null;
    this.openTipsDialog = function(content){
        var contentIdx = 0;
        var content = content || QSTIPs;
		var width = xDesktopW<980?xDesktopW:980;
        var height = xDesktopH<850?xDesktopH:850;//xDesktopH * 0.75;
        var tipsTimer;
        if (!rtqWindowGroup) {
            rtqWindowGroup = new Util.WindowGroup(document.body, {}, {});
        }
        if (!TipsWin) {
            TipsWin = new Util.RtqWindow(document.body, {
                windowReady: function(obj){
                    var qslang=QS_Lang.workspace;
					var QS_TIPS_BTN_TXT=Terminal.getConfig('HideLicenseDialog')?qslang["dg_dclose"]:qslang["dg_dstart"];//new user opens QS terminal first time. added by wind @ 20120306
                    obj.setHtml('<div id="rtq-tipsdialog"><div class="content"><div class="data"></div></div><div class="action"><a class="qs-ui-btn close" href="javascript:void(0)">'+QS_TIPS_BTN_TXT+'</a><div class="page"><a class="qs-ui-btn prev" href="#">&lt;</a><span>0/0</span><a class="qs-ui-btn next" href="#">&gt;</a></div><label><input name="" value="" type="checkbox" />'+qslang["dg_dtipprompt"]+'</label></div></div>');
                    var contentIdx = 0;
                    var El = obj.el;
                	El.find('.content').height(height - 80);
                    var oNext = $('.page .next', El);
                    var oPrev = $('.page .prev', El);
                    var oClose = $('.close', El);
                    var contentEl = $('.content .data', El);
                    var chk = $('.action input', El);
                    var pageinfo = $('.page span', El).html((contentIdx + 1) + '/' + QSTIPs.length);
                    contentEl.html(QSTIPs[0]['content']);
                    if (Terminal.getConfig('HideTipsDialog') && Terminal.getConfig('HideTipsDialog') == true) {
                        chk[0].checked = true;
                    }
                    var dataScrollPN = new Util.ScrollPanel(contentEl, {
                        showX: false
                    });
                    dataScrollPN.reSize();
                    oNext.bind('click.tips', function(e){
                        contentIdx = (contentIdx >= QSTIPs.length - 1) ? contentIdx : contentIdx + 1;
                        tipsTimer = setTimeout(function(){
                            contentEl.html(QSTIPs[contentIdx]['content']);
							TipsWin.setTitle(QSTIPs[contentIdx]['title']);
                            dataScrollPN.reSize();
                        }, 500);
                        pageinfo.html((contentIdx + 1) + '/' + QSTIPs.length);
                    });
                    oPrev.bind('click.tips', function(e){
                        contentIdx = (contentIdx <= 0) ? 0 : contentIdx - 1;
                        tipsTimer = setTimeout(function(){
                            contentEl.html(QSTIPs[contentIdx]['content']);
							TipsWin.setTitle(QSTIPs[contentIdx]['title']);
                            dataScrollPN.reSize();
                        }, 500);
                        pageinfo.html((contentIdx + 1) + '/' + QSTIPs.length);
                    });
                    oClose.bind('click.tips', function(e){
                        Terminal.setConfig({
                            'HideTipsDialog': chk[0].checked
                        });
                        obj.hide();
						if(!Terminal.getConfig('HideLicenseDialog')){
							workspaceManager.openWorkspaceManager();//new user opens QS terminal first time. added by wind @ 20120306
						}						
                    });
                },
                beforeClosed: function(){
                }
            }, {
                title: QSTIPs[0]['title'],
                top: 30,
                width: width,
                height: height,
                resizable: false,
                isGlobal: true,
                hasSettingPanel: false,
                closeAction: 'hide'
            });
            rtqWindowGroup.register(TipsWin);
        }
        else {
            TipsWin.show();
            clearTimeout(tipsTimer);
        }
    };
    //News detail window
    var NewsDetailWin = null;
    var NewsScrollPN = null;
    this.openNewsDetailWin = function(title, content, url){
        if (!rtqWindowGroup) {
            rtqWindowGroup = new Util.WindowGroup(document.body, {}, {});
        }
        if (!NewsDetailWin) {
            NewsDetailWin = new Util.RtqWindow(document.body, {
                windowReady: function(obj){
                    obj.url = url;
                    obj.setHtml('<div class="rtq-newsdetail"><div class="content"><div class="data"></div></div><div class="action"><a class="qs-ui-btn close" href="javascript:void(0)">'+QS_Lang.workspace["dg_dclose"]+'</a></div></div>');
                    var El = obj.el;
                    var close = $('a.close', El);
                    var contentEl = El.find('.content');
                    var dataEl = $('.content .data', El);
                    if (NewsScrollPN == null) {
                        NewsScrollPN = new Util.ScrollPanel(dataEl);
                    }
                    if (!obj.url) {
                        NewsScrollPN.el.show();
                        dataEl.html(content);
                        dataEl.find('a').attr('target', '_blank');
                        dataEl.css({
                            height: 410
                        });
                        //resize until img loaded 
						var imgCount=0;
						var len=dataEl.find('img').length;
						if (len > 0) {
							dataEl.find('img').bind('load', function(e){
								imgCount++;
								if(imgCount==len){
									NewsScrollPN.reSize(660, 410);
								}
							}).bind('error', function(){
							    imgCount++;
                                if(imgCount==len){
                                    NewsScrollPN.reSize(660, 410);
                                }
							});
						}else{
							NewsScrollPN.reSize(660, 410);
						}
                    }
                    else {
                        NewsScrollPN.el.hide();
                        dataEl.appendTo(contentEl);
                        dataEl.html(content + '<br><iframe frameborder="0" src="' + obj.url + '" style="width:660px;height:380px"></iframe>');
                    }
                    close.bind("click.newsDetail", function(e){
                        obj.hide();
                    });
                },
                beforeClosed: function(){
                },
                onResized: function(obj, w, h){
                    var contentEl = obj.el.find('.content');
                    var dataEl = obj.el.find('.content .data');
                    contentEl.css({
                        height: h - 90
                    });
                    dataEl.css({
                        width: w - 40,
                        height: h - 90
                    });
                    if (!obj.url) {
                        NewsScrollPN.reSize(w - 40, h - 90);
                    }
                    else {
                        dataEl.find('iframe').css({
                            width: w - 40,
                            height: h - 120
                        });
                    }
                }
            }, {
                title: title,
                top: 80,
                width: 700,
                height: 500,
				oldBox: {width:700,height:500},
                resizable: true,
                isGlobal: true,
                hasSettingPanel: false,
                closeAction: 'hide'
            });
            rtqWindowGroup.register(NewsDetailWin);
        }
        else {
            NewsDetailWin.show();
            NewsDetailWin.setTitle(title);
            NewsDetailWin.url = url;
            var el = NewsDetailWin.el, w = el.width(), h = el.height();
            var contentEl = el.find('.content');
            var dataEl = $('.content .data', el);
            if (!NewsDetailWin.url) {
                NewsScrollPN.el.show();
                dataEl.appendTo(NewsScrollPN.el);
                dataEl.html(content);
                dataEl.find('a').attr('target', '_blank');
                dataEl.css({
                    height: h - 90
                });
				 //resize until img loaded 
                var imgCount=0;
                var len=dataEl.find('img').length;
                if (len > 0) {
                    dataEl.find('img').bind('load', function(e){
                        imgCount++;
                        if(imgCount==len){
                            NewsScrollPN.reSize(w - 40, h - 90);
                        }
                    }).bind('error', function(){
                        imgCount++;
                        if(imgCount==len){
                            NewsScrollPN.reSize(w - 40, h - 90);
                        }
                    });
                }else{
                    NewsScrollPN.reSize(w - 40, h - 90);
                }
            }
            else {
                NewsScrollPN.el.hide();
                dataEl.appendTo(contentEl);
                dataEl.css({
                    marginRight: 0,
                    width: w - 40
                });
                dataEl.html(content + '<br><iframe frameborder="0" src="' + NewsDetailWin.url + '"></iframe>');
                dataEl.find('iframe').css({
                    width: w - 40,
                    height: h - 120
                });
            }
        }
    };
	
	/*
	 *online support window 
	*/
	var supportWin=null;
	this.openOnlineSupportDialog = function(url){
		if (!supportWin) {
            var qslang=QS_Lang.workspace;
			supportWin = new Util.RtqWindow(document.body, {
				windowReady: function(obj){
					obj.setHtml('<iframe id="onlineSupport" name="onlineSupport" class="" src="' + url + '" style="width:100%;height:100%;border:none;" frameborder="0"></iframe>');
				},
				beforeClosed: function(){
					supportWin=null;
				},
				onResized:function(obj,w,h){
					obj.el.find(".win-bd").height(h-22);
					var ifm = window.frames["onlineSupport"];
					document.domain="morningstar.com";
					ifm.qsOnlineSupport.changeOutSize(obj,w,h);
				}
			}, {
				title: qslang["dg_dsupport"],
				width: 650,
				height: 500,
				oldBox: {width:650,height:500},
				resizable: true,
				isGlobal: true,
				hasSettingPanel: false,
				closeAction: 'hide'
			});
			var title=supportWin.el.find(".win-hd h3");
			title.append("<span style='position:relative;float:right;margin-right:40px;'>"+qslang["dg_dsupportprompt"]+"<a href='mailto:qs@morningstar.com'  style='text-decoration:underline;'>"+qslang["dg_dmail"]+"</a></span>");
			rtqWindowGroup.register(supportWin);	
					
			supportWin.el.find(".win-bd").height(supportWin.el.find(".win-bd").height()+4);
		}else{
			supportWin.show();
		}		
    };
};
